/**  
 * All rights Reserved, Designed By www.woniuxy.com
 * @Title:  Account.java   
 * @Package com.woniu.domain   
 * @Description:    Description  
 * @author: 蜗牛学院  
 * @date:   2019年7月30日 下午3:00:14
 * @user:   Administrator  
 * @version V1.0 
 * @Copyright: 2019 www.woniuxy.com Inc. All rights reserved. 
 * 注意：本内容仅限于成都蜗牛创想科技有限公司，禁止外泄以及用于其他的商业目
 */ 
package com.woniu.dao.impl;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.woniu.dao.UserDao;
import com.woniu.domain.User;
import com.woniu.domain.UserAddress;
import com.woniu.utils.DataSourceUtils;
/**
 * @author Administrator
 *
 */


public class UserDaoImpl implements UserDao {

	private QueryRunner runner;

	public UserDaoImpl() {
		runner = new QueryRunner(DataSourceUtils.getDataSource());
	}

	@Override
	public User findUserByuNameAnduPwd(String uName, String uPwd) throws SQLException {
		return runner.query("select uname,upwd from User where uname = ? and upwd =?", new BeanHandler<User>(User.class),uName,uPwd);
	}

	@Override
	public User findUserName(String uName) throws SQLException {
		
		return runner.query("select uname from user where uname = ?", new BeanHandler<User>(User.class),uName);
	}

	@Override
	public int saveUser(User user) throws SQLException {
		
		return runner.execute("insert into user values(?,?,?,?,?,?,?)", user.getuId(),user.getuName(),user.getuPwd(),user.getPhoneNum(),user.getEmail(),user.getRegisterTime(),user.getLogoutTime());
	}

	@Override
	public List<UserAddress> findUserAddress(User user) throws SQLException {
		return runner.query("select aid,uname,phonenum,address from address where aid = ?",new BeanListHandler<UserAddress>(UserAddress.class),user.getuId());
	}

	@Override
	public User findUser() throws SQLException {
		return runner.query("select * from user", new BeanHandler<User>(User.class));
	}

	@Override
	public void addAddress(String uName, String phoneNum, String province, String city, String county, String street,
			String detail,User user) throws SQLException {
		runner.execute("insert into address values (?,?,?,?)", user.getuId(),uName,phoneNum,province+city+county+street+detail);
		
	}
}

